import utils.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @Author ZhangCuirong
 * @Date 2025/9/3 12:43
 * @description:
 */
public class Solution543 {
    int maxDiameter =0 ;
    public int diameterOfBinaryTree(TreeNode root) {
        if (root == null) {
            return 0;
        }
        traverse(root);
        return maxDiameter;
    }
    public void traverse(TreeNode root){
        if(root ==null){
            return;
        }
        int leftMax = maxDepth(root.left);
        int rightMax = maxDepth(root.right);
        int myDiameter = leftMax + rightMax;
        maxDiameter = Math.max(maxDiameter,myDiameter);
        traverse(root.left);
        traverse(root.right);

    }

    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftMaxDepth = root.left != null ? maxDepth(root.left) : 0;
        int rightMaxDepth = root.right != null ? maxDepth(root.right) : 0;
        return Math.max(leftMaxDepth, rightMaxDepth) + 1;
    }
}
